import os, os.path

from puppy.service import ServiceManager, SearchService
from puppy.search.engine import Bing, YouTubeV2, Flickr
from puppy.search.engine.whooshQueryEngine import WhooshQueryEngine
from puppy.query.filter.whooshQueryLogger import WhooshQueryLogger
from puppy.logging import QueryLogger
from puppy.query.filter import BlackListFilter

from settings import ONDOLLEMAN, DOLLEMANPATH

log_dir = "main/query_logs"

if ONDOLLEMAN:
    my_whoosh_query_index_dir = os.path.join(DOLLEMANPATH, "main/query_logs/index")
    log_dir = os.path.join(DOLLEMANPATH, log_dir)
else:
    my_whoosh_query_index_dir = os.path.join(os.getcwd(), "main/query_logs/index")

config = {
 #"proxyhost": "http://wwwcache.gla.ac.uk:8080", # <-- remove if not UGLW
 "log_dir": log_dir,
 "bing_api_key": "FA5E0EB846B909111D77EDA50D180AD1145506B3",
 "flickr_api_key": "8b2474d3d94c0159001ec484a6731d9b",
}

# create a ServiceManager
service = ServiceManager(config)

# create a SearchService, choose search engine and enable query logging
news_search_service = SearchService(service, "news_search")
news_search_service.search_engine = Bing(news_search_service, source='news', resultsPerPage = 9)

# add SearchService to ServiceManager
service.add_search_service(news_search_service)

# create a SearchService, choose search engine and enable query logging
video_search_service = SearchService(service, "video_search")
video_search_service.search_engine = YouTubeV2(video_search_service, resultsPerPage = 3)

# add SearchService to ServiceManager
service.add_search_service(video_search_service)

# create a SearchService, choose search engine and enable query logging
picture_search_service = SearchService(service, "picture_search")
picture_search_service.search_engine = Flickr(picture_search_service, resultsPerPage = 3)

# add SearchService to ServiceManager
service.add_search_service(picture_search_service)


# Create a Whoosh Query Logger that records all the unique (it doesn't record duplicates) queries
whoosh_query_logger = WhooshQueryLogger(whoosh_query_index_dir=my_whoosh_query_index_dir, unique=True)

# create a SearchService, called 'query_suggest_search'
query_suggest_search_service = SearchService(service, "query_suggest_search")

# Set our SearchService to use the Whoosh Query Engine, setting it to look in the directory we set the Query Logger to record queries in
query_suggest_search_service.search_engine = WhooshQueryEngine(service=query_suggest_search_service, whoosh_query_index_dir=my_whoosh_query_index_dir)

# add SearchService to our ServiceManager (this handles all the search services MaSe contains - now 'Web', 'Image' and 'Suggestions)
service.add_search_service(query_suggest_search_service)
